﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using ElectronicCommerce.Framework.Web.UI.GeneratedImage.ImageTransform;

namespace ElectronicCommerce.Framework.Web.UI.GeneratedImage
{
    public class ImageSizeHelper
    {
        private static Size SizeBaseConvert(Image originalSize, Size templateSize, int times)
        {
            Size newSize = new Size();
            float rate = (float)originalSize.Height / originalSize.Width;
            int newWidth = originalSize.Width * times;
            int newHeight = originalSize.Height * times;

            if (rate <= 0.75 && newWidth > templateSize.Width)
            {
                newSize.Width = templateSize.Width;
                newSize.Height = (int)(originalSize.Height * templateSize.Width / originalSize.Width);
            }
            else if (rate > 0.75 && newHeight > templateSize.Height)
            {
                newSize.Width = (int)(originalSize.Width * templateSize.Height / originalSize.Height);
                newSize.Height = templateSize.Height;
            }
            else
            {
                newSize.Width = originalSize.Width * times;
                newSize.Height = originalSize.Height * times;
            }

            return newSize;
        }

        private static Size Convert(Image originalSize, Size templateSize, int times)
        {
            Size newSize = new Size();
            newSize.Height = originalSize.Height;
            newSize.Width = originalSize.Width;
            float rate = (float)originalSize.Height / originalSize.Width;
            if (times == 1)
            {
                if (rate <= 0.75)
                {
                    newSize.Width = (int)(originalSize.Width * templateSize.Width / originalSize.Width);
                    newSize.Height = (int)(originalSize.Height * templateSize.Width / originalSize.Width);
                }
                else
                {
                    newSize.Width = (int)(originalSize.Width * templateSize.Height / originalSize.Height);
                    newSize.Height = (int)(originalSize.Height * templateSize.Height / originalSize.Height);
                }
            }
            else if (times > 1)
            {
                if (rate <= 0.75)
                {
                    if (originalSize.Width <= templateSize.Width / times)
                    {
                        newSize.Width = newSize.Width * times;
                    }
                    else
                    {
                        newSize.Width = templateSize.Width;
                    }
                }
                else
                {
                    if (originalSize.Height <= templateSize.Height / times)
                    {
                        newSize.Height = newSize.Height * times;
                    }
                    else
                    {
                        newSize.Height = templateSize.Height;
                    }
                }
            }
            return newSize;
        }


        private static Size ToOrgSize(Image orgImg)
        {
            Size newSize = new Size();
            newSize.Height = orgImg.Height;
            newSize.Width = orgImg.Width;

            return newSize;
        }

        public static Size Resize(Image orgImg, Size reqSize)
        {
            //if new size equals 0 which means use original size
            if (reqSize.Height == 0 || reqSize.Width == 0) return ToOrgSize(orgImg);

            //if new size ratio equals originals use new size directly
            if (reqSize.Width / reqSize.Height == orgImg.Width / orgImg.Height)
            {
                return reqSize;
            }

            //old logic
            if (orgImg.Height >= reqSize.Height || orgImg.Width >= reqSize.Width)
                return SizeBaseConvert(orgImg, reqSize, 1);
            else if (orgImg.HorizontalResolution <= 72)
                return Convert(orgImg, reqSize, 0);
            else if (orgImg.HorizontalResolution < 250)
                return SizeBaseConvert(orgImg, reqSize, 2);
            else
                return SizeBaseConvert(orgImg, reqSize, 3);
        }
    }
}
